package web;

import Utils.JDBCUtils;
import Utils.JSONUtils;
import beans.RegisterStatus;
import beans.User;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import service.registerService;

import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(value = "/user/register")
public class registerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        User user = JSONUtil.toBean(stringBuilder.toString(), User.class);
        registerService registerService = new registerService();
        boolean register = registerService.register(user);
        if (register) {
            response.getWriter().write(JSONUtils.result(RegisterStatus.REGISTER_SUCCESS,user));
        }
        else {
            response.getWriter().write(JSONUtils.result(RegisterStatus.REGISTER_FAIL,null));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
